package com.hivision.aladdin.codec;

import java.io.IOException;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;

public class CmdProcess {

	private String identifier = "123";
	private String msgType;
	private String serviceId = "SmokeDetectorBasic";
//	private String cmd = "SET_DEVICE_LEVEL";
	private int hasMore = 0;
	private int errcode = 0;
	private int mid = 0;
	private JsonNode paras;

	private String uid; // 设备uid
	private int cmdCode; // 指令码

	private int mainVersion = 0x00; // 主版本
	private int subVersion = 0x00; // 副版本
	private final int RSP_LEN = 19;
	private final int CMDCODE_INDEX = 16; // 指令码字节的位置索引
	
	private byte[] request; // 设备上报的数据

	public CmdProcess() {
	}

	public CmdProcess(ObjectNode input) {

		try {
			this.identifier = input.get("identifier").asText();

			this.msgType = input.get("msgType").asText();
//			this.serviceId = input.get("serviceId").asText();
//			this.cmd = input.get("cmd").asText();
//			this.mid = input.get("mid").intValue();
//			this.paras = input.get("paras");
			this.hasMore = 0;//input.get("hasMore").asInt();
			
//			if(this.paras.get("parameters").has("mainVersion")){
//				this.mainVersion = this.paras.get("parameters").get("mainVersion").asInt();
//			}
//			if(this.paras.get("parameters").has("subVersion")){
//				this.subVersion = this.paras.get("parameters").get("subVersion").asInt();
//			}
//			
//			this.uid = this.paras.get("parameters").get("uid").asText();

			// 如果是应答
			if (msgType.equals("cloudRsp")) {
				this.request = Utilty.hexString2Bytes(input.get("request").asText());
	        	
				// 在此组装ACK的值
				this.errcode = input.get("errcode").asInt();
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

//	private int setBaseValue(byte[] sendByte) throws Exception {
//
//		int i = 0;
//		// 帧头
//		sendByte[i++] = (byte) '@';
//		sendByte[i++] = (byte) '#';
//		// 消息长度
//		byte[] byteLen = Utilty.getInstance().int2Bytes(reqLength-4, 2);
//		sendByte[i++] = byteLen[0];
//		sendByte[i++] = byteLen[1];
//		// 主版本
//		sendByte[i++] = (byte) mainVersion;
//		// 副版本
//		sendByte[i++] = (byte) subVersion;
//		// 单灯节点UID
//		byte[] byteUid = this.uid.getBytes();
//		for (byte b : byteUid) {
//			sendByte[i++] = b;
//		}
//		// 消息id
//		byte[] byteMid = Utilty.getInstance().int2Bytes(mid, 2);
//		sendByte[i++] = byteMid[0];
//		sendByte[i++] = byteMid[1];
//		// 指令码
//		byte[] byteCmdCode = Utilty.getInstance().int2Bytes(cmdCode, 2);
//		sendByte[i++] = byteCmdCode[0];
//		sendByte[i++] = byteCmdCode[1];
//		
//		return i;
//	}
	
	public byte[] toByte() {
		try {
			if (("cloudRsp").equals(this.msgType)) {
//				byte[] ack = new byte[RSP_LEN];
//				int i=setBaseValue(ack);
//				// 执行结果
//				ack[i++] = (byte) this.errcode;
//				// CRC校验
//				int crc = CRC16M.calculateCrc16(ack, ack.length - 2);
//				byte[] byteCrc = Utilty.getInstance().int2Bytes(crc, 2);
//				ack[i++] = byteCrc[0];
//				ack[i++] = byteCrc[1];
//
//				return ack;
				// 指令码
	        	int requestCmdCode = Utilty.getInstance().bytes2Int(this.request, CMDCODE_INDEX, 2);
	        	if(requestCmdCode == 0x0002){
	        		byte[] byteCmdCode = Utilty.getInstance().int2Bytes(0x0102, 2);
	        		request[CMDCODE_INDEX] = byteCmdCode[0];
	        		request[CMDCODE_INDEX + 1] = byteCmdCode[1];
	        		
		        	// CRC校验
					int crc = CRC16M.calculateCrc16(request, request.length - 2);
					byte[] byteCrc = Utilty.getInstance().int2Bytes(crc, 2);
					request[request.length-2] = byteCrc[0];
					request[request.length-1] = byteCrc[1];
					
					return request;
	        	}
			}
			return null;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return null;
		}
	}
	
	public static void main(String[] args) {
		ObjectMapper mapper = new ObjectMapper();
		String js = "{\"identifier\": \"123\",\"msgType\": \"cloudReq\",\"serviceId\": \"SmokeDetectorBasic\",\"cmd\": \"SET_BRIGHTNESS\",\"mid\": 2016,\"paras\": {\"parameters\":{\"uid\":\"12345678\", \"brightness\": \"10\", \"cmdCode\":4}},\"hasMore\": 0}";
		
		ObjectNode cmd = mapper.createObjectNode();
//		cmd.put("identifier", "123");
//		cmd.put("msgType", "deviceReq");
//		cmd.put("serviceId", "SmokeDetectorBasic");
		
	    cmd.put("identifier",  "12345678");
	    cmd.put("msgType",  "cloudRsp");
	    cmd.put("request",  "4023110000001122334455667788000002000E0BFC");
	    cmd.put("errcode",  0);
	    cmd.put("hasMore",  0);
//	    cmd.put("uid",  "12334");
	    System.out.println(cmd.toString());
		
		try {
			ObjectNode readNode = (ObjectNode)mapper.readTree(js);
			CmdProcess cmdProcess = new CmdProcess(cmd);
			byte[] b = cmdProcess.toByte();
			System.out.println(Utilty.parseByte2HexStr(b));
			
//			byte[] byteRead = Utilty.hexString2Bytes("4023110000001122334455667788000004001E");
//			int crc = CRC16M.calculateCrc16(byteRead, byteRead.length - 2);
//			byte[] byteCrc = Utilty.getInstance().int2Bytes(crc, 2);
//			
//			
//			byte[] t = Utilty.hexString2Bytes("4023110000001122334455667788000002001E0BFC");
//			
//			ReportProcess reportProcess = new ReportProcess(t);
//			ObjectNode parseNode = reportProcess.toJsonNode();
//			System.out.println(parseNode.toString());
		} catch (JsonProcessingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

